﻿using AnyCAD.Foundation;
using Avalonia.Controls;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace FeatureRecognization
{
    internal partial class MainViewModel : ObservableObject
    {
        public ObservableCollection<TreeViewItem> FeatureItems { get; set; } = new ObservableCollection<TreeViewItem>();

        IRenderView mRenderView;
        Dictionary<string, Vector3> mFeatureColorTable = new Dictionary<string, Vector3>();
        public MainViewModel(IRenderView renderView) 
        {
            mRenderView = renderView;

            mFeatureColorTable.Add("Outter", ColorTable.LightSeaGreen);
            mFeatureColorTable.Add("Inner", ColorTable.LightSkyBlue);
            mFeatureColorTable.Add("Hole", ColorTable.LightCoral);
        }

        [RelayCommand]
        void OnOpenModel()
        {
            // 打开文件
            var fullPath = DialogUtil.OpenFileDialog("Model", new StringList(new string[] { "Model Files (.step .stp .iges .igs)", "*.step *.stp *.iges *.igs" }));
            if (fullPath.IsEmpty())
                return;

            var shape = ShapeIO.Open(fullPath);
            if (shape == null)
                return;

            // 特征识别
            var featureContext = new FeatureContext(shape);
            FeatureEngine engine = new FeatureEngine();
            if (!engine.Recognize(featureContext, new StringList { "Tube", "Section" }))
                return;

            var node = mRenderView.ShowShape(featureContext.GetSolid(), ColorTable.Gray);

            // 收集特征识别结果
            var shapeTreeNode = new TreeViewItem();
            shapeTreeNode.Header = System.IO.Path.GetFileName(fullPath.ToA());
            shapeTreeNode.Tag = node.GetUuid();
            CollectFeatures(featureContext, node, shapeTreeNode);

            FeatureItems.Add(shapeTreeNode);

            mRenderView.ZoomAll();
        }
        [RelayCommand]
        void OnOpenHole()
        {
            // 打开文件
            var fullPath = DialogUtil.OpenFileDialog("Model", new StringList(new string[] { "Model Files (.step .stp .iges .igs)", "*.step *.stp *.iges *.igs" }));
            if (fullPath.IsEmpty())
                return;

            var shape = ShapeIO.Open(fullPath);
            if (shape == null)
                return;

            // 特征识别
            var featureContext = new FeatureContext(shape);
            FeatureEngine engine = new FeatureEngine();
            if (!engine.Recognize(featureContext, new StringList { "Hole"}))
                return;

            var node = mRenderView.ShowShape(featureContext.GetSolid(), ColorTable.Gray);

            // 收集特征识别结果
            var shapeTreeNode = new TreeViewItem();
            shapeTreeNode.Header = System.IO.Path.GetFileName(fullPath.ToA());
            shapeTreeNode.Tag = node.GetUuid();
            CollectFeatures(featureContext, node, shapeTreeNode);

            FeatureItems.Add(shapeTreeNode);

            mRenderView.ZoomAll();
        }
        /// <summary>
        /// 根据类型返回颜色
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        Vector3 GetColor(string name)
        {
            Vector3 v = ColorTable.Red;
            mFeatureColorTable.TryGetValue(name, out v);
            return v;
        }

        /// <summary>
        /// 收集特征
        /// </summary>
        /// <param name="featureContext"></param>
        /// <param name="node"></param>
        /// <param name="treeItem"></param>
        void CollectFeatures(FeatureContext featureContext, BrepSceneNode node, TreeViewItem treeItem)
        {
            var treeGroup = new ObservableCollection<TreeViewItem>();

            // 遍历所有组
            foreach (var group in featureContext.GetGroups())
            {
                var featureTreeItem = new TreeViewItem();
                featureTreeItem.Header = group.GetName();
                featureTreeItem.Tag = group.GetName();

                // 遍历所有特征
                var children = new ObservableCollection<TreeViewItem>();
                foreach (var item in group.GetItems())
                {
                    var subTreeItem = new TreeViewItem();
                    subTreeItem.Header = item.GetName();
                    subTreeItem.Tag = item.GetName();

                    children.Add(subTreeItem);

                    // 遍历特征上的面信息
                    foreach(var faceGroupName in item.GetFaceGroupNames())
                    {
                        var faceIds = item.GetFaces(faceGroupName);
                        foreach(var faceId in faceIds)
                        {
                            node.SetFaceColor(faceId, GetColor(item.GetName()));
                        }
                    }
                }
                featureTreeItem.ItemsSource = children;

                treeGroup.Add(featureTreeItem);
            }
            treeItem.ItemsSource = treeGroup;
        }
    }
}
